package com.wangyonghao.harmonyocrs.provider;

import com.google.gson.Gson;
import com.wangyonghao.harmonyocrs.CameraAbility;
import com.wangyonghao.harmonyocrs.MainAbility;
import com.wangyonghao.harmonyocrs.ResourceTable;
import com.wangyonghao.harmonyocrs.pojo.res.RecogResult;
import com.wangyonghao.harmonyocrs.slice.HistoryAbilitySlice;
import com.wangyonghao.harmonyocrs.utils.HttpUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;

import java.util.List;

public class RecogProvider extends BaseItemProvider {
    Gson gson = new Gson();

    private List<RecogResult> list;
    private HistoryAbilitySlice as;

    public RecogProvider(List<RecogResult> list, HistoryAbilitySlice as) {
        this.list = list;
        this.as = as;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    //返回RecogResult中婴加载的布局对象
    //参数一:当前要加载哪- -行 RecogResult (RecogResult的索引 )
    //参数二:优化
    //参数三:优化
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        DirectionalLayout dl;
        if (component != null) {
            dl = (DirectionalLayout) component;
        } else {
            //获取每一个RecogResult中的布局对象
            dl = (DirectionalLayout) LayoutScatter.getInstance(as).parse(ResourceTable.Layout_itemview, null, false);
        }
        RecogResult history = list.get(i);
        Image img = (Image) dl.findComponentById(ResourceTable.Id_img);
        if ("camera".equals(history.getWay())) {
            img.setImageAndDecodeBounds(ResourceTable.Media_camera);
        } else {
            img.setImageAndDecodeBounds(ResourceTable.Media_picture);
        }
        Text content = (Text) dl.findComponentById(ResourceTable.Id_content);
        content.setText(history.getContent());
        Text time = (Text) dl.findComponentById(ResourceTable.Id_time);
//        time.setText(DateUtils.datetimeToString(history.getRecog_time()));
        time.setText(history.getRecog_time());
        Image del = (Image) dl.findComponentById(ResourceTable.Id_del);
        del.setTag(history.getId());
        del.setClickedListener(e -> {
            Integer id = (Integer) del.getTag();
            HttpUtils httpUtils = new HttpUtils();
            String s = httpUtils.deleteToFormURL("/recog/basic/delete/"+id, null);
            as.getData();
        });
        dl.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                intent.setParam("time", history.getRecog_time());
                intent.setParam("way", history.getWay());
                intent.setParam("result", history.getContent());
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.wangyonghao.harmonyocrs")
                        .withAbilityName(MainAbility.class.getName())
                        .withAction("historyDetailAbilitySlice")
                        .build();
                intent.setOperation(operation);
                as.startAbility(intent);
            }
        });
        return dl;
    }

    public List<RecogResult> getList() {
        return list;
    }

    public void setList(List<RecogResult> list) {
        this.list = list;
    }

    public AbilitySlice getAs() {
        return as;
    }

    public void setAs(HistoryAbilitySlice as) {
        this.as = as;
    }
}
